package com.haotunet.app.goldtreasure.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.ProductItemEntity;
import com.haotunet.app.goldtreasure.model.RecommendProductBean;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;

import java.util.List;

public class ProductAdapter extends BaseAdapter {
    private List<ProductItemEntity> list;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public ProductAdapter(Context context, List<ProductItemEntity> mList) {
        this.mContext = context;
        this.list = mList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (null != list) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder = null;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_product, null);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.product_tag);
            viewHolder.titleLayout = (LinearLayout) convertView.findViewById(R.id.title_layout);
            viewHolder.tvRate = (TextView) convertView.findViewById(R.id.tv_regular_rate);
            viewHolder.tvIntroduction = (TextView) convertView.findViewById(R.id.tv_regular_introduction);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_regular_name);
            viewHolder.buyCondition = (TextView) convertView.findViewById(R.id.buy_condition);
            viewHolder.profitTxt = (TextView) convertView.findViewById(R.id.profit_txt);
            viewHolder.buyStateLogo = (ImageView) convertView.findViewById(R.id.product_buy_state_logo);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // 获取数据
        ProductItemEntity itemEntity = (ProductItemEntity) getItem(position);
        RecommendProductBean recommendProductBean = itemEntity.getRecommendProductBean();
        final int productFlag = recommendProductBean.getProductType();
        final String buyGoldH5url = recommendProductBean.getBuyGoldH5url();
        String rateExtDesc = recommendProductBean.getRateExtDesc();
        if (productFlag == 2) {
            SpannableStringBuilder style = new SpannableStringBuilder(rateExtDesc);
            style.setSpan(new RelativeSizeSpan(0.5f), 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            style.setSpan(new RelativeSizeSpan(0.5f), rateExtDesc.length() - 3, rateExtDesc.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            viewHolder.tvRate.setText(style);
            viewHolder.profitTxt.setVisibility(View.GONE);
        } else if (productFlag == 4) {
            SpannableStringBuilder style1 = new SpannableStringBuilder(rateExtDesc);
            style1.setSpan(new RelativeSizeSpan(0.8f), 0, rateExtDesc.length() - 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            style1.setSpan(new RelativeSizeSpan(0.6f), rateExtDesc.length() - 1, rateExtDesc.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            viewHolder.tvRate.setText(style1);
            viewHolder.profitTxt.setVisibility(View.VISIBLE);
        } else {
            SpannableStringBuilder style2 = new SpannableStringBuilder(rateExtDesc);
            style2.setSpan(new RelativeSizeSpan(0.6f), rateExtDesc.length() - 1, rateExtDesc.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            viewHolder.tvRate.setText(style2);
            viewHolder.profitTxt.setVisibility(View.VISIBLE);
        }
        viewHolder.tvName.setText(recommendProductBean.getName());
        viewHolder.profitTxt.setText(recommendProductBean.getRateIntroduction());
        viewHolder.tvIntroduction.setText(recommendProductBean.getIntroduction());
        viewHolder.buyCondition.setText(recommendProductBean.getBaseDesc());
        if (recommendProductBean.getIsDisable()) {
            viewHolder.buyStateLogo.setBackgroundResource(R.drawable.sold_out_icon);
        } else {
            viewHolder.buyStateLogo.setBackgroundResource(R.drawable.buy_logo);
        }
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityUtils.gotoDynamicDetail(mContext, buyGoldH5url, 11);
            }
        });

        if (needTitle(position)) {
            // 显示标题并设置内容
            viewHolder.title.setText(itemEntity.getmTitle());
            viewHolder.titleLayout.setVisibility(View.VISIBLE);
        } else {
            // 内容项隐藏标题
            viewHolder.titleLayout.setVisibility(View.GONE);
        }

        return convertView;

    }

    /**
     * 判断是否需要显示标题
     *
     * @param position
     * @return
     */
    private boolean needTitle(int position) {
        // 第一个肯定是分类
        if (position == 0) {
            return false;
        }

        // 边界处理
        if (position < 0) {
            return false;
        }

        // 当前 // 上一个
        ProductItemEntity currentEntity = (ProductItemEntity) getItem(position);
        ProductItemEntity previousEntity = (ProductItemEntity) getItem(position - 1);
        if (null == currentEntity || null == previousEntity) {
            return false;
        }

        String currentTitle = currentEntity.getmTitle();
        String previousTitle = previousEntity.getmTitle();
        if (null == previousTitle || null == currentTitle) {
            return false;
        }

        // 当前item分类名和上一个item分类名不同，则表示两item属于不同分类
        if (currentTitle.equals(previousTitle)) {
            return false;
        }

        return true;
    }

    public static class ViewHolder {
        TextView title;
        LinearLayout titleLayout;
        TextView tvName;
        TextView buyCondition;
        TextView tvIntroduction;
        TextView tvRate;
        TextView profitTxt;
        ImageView buyStateLogo;
    }
}